<?php
/*
 * EImageFinder widget
 * Based on CKFinder (http://ckfinder.com/)
 *
 * @usage $this->widget('ext.finder.EImageFinder',array('field'=>'my_field'));
 *
 * @author: Cassiano Surek <cass@surek.co.uk>
 */

class EImageFinder extends CInputWidget
{
        public $field;
        public $field_name;//field name
        private $uploadPath;
        private $uploadUrl;
        protected $path;

    public function init()
    {
        // Please change the config below to suit your needs
        $this->uploadPath = dirname(Yii::app()->request->scriptFile).'/files/';
        $this->uploadUrl = Yii::app()->getRequest()->hostInfo. Yii::app()->baseUrl.'/files/';

        // We need to make the CKFinder accessible, let's publish it to the assets folder
        /**
         * @trongkhoi made this for run at shared hosting
         */
        $lo_am = new CAssetManager;
        $lo_am->newFileMode=0644;
        $lo_am->newDirMode=0755;
        $this->path = $lo_am->publish(Yii::app()->basePath . '/extensions/ckeditor/assets',true);
        
        // And save the upload path to use with ckfinder's config file. Passing as js param did not work...
        $lo_session=new CHttpSession;
        $lo_session->open();
        $lo_session['auth']=true;
        $lo_session['upload_path'] = $this->uploadPath;
        $lo_session['upload_url'] = $this->uploadUrl;

        parent::init();
    }

    public function run()
    {
        $this->render("ckfinder", array(
                'field'=>$this->field,
                'field_name'=>$this->field_name,
                'path'=>$this->path,
            ));
    }
}